package com.pan.note.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * webmvc配置类
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;

    /**
     * 配置模版引擎加载静态资源
     * @param registry 静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 模版获取静态资源
        registry.addResourceHandler("static/**")
                .addResourceLocations("classpath:/static/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    /**
     * 定义页面路由
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main.html").setViewName("dashboard");
        registry.addViewController("/user.html").setViewName("list");
        registry.addViewController("/write.html").setViewName("write");
        registry.addViewController("/our.html").setViewName("our");
        registry.addViewController("/me.html").setViewName("me");
        registry.addViewController("/test.html").setViewName("test");
    }

    /**
     * 添加自定义拦截器，使其生效
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginHandlerInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/note/user/login");
    }
}
